import java.util.List;

class ListNode{
    public int val;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}

public class SingleLinkedList {

    public ListNode head;

    public void createList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display(){
        ListNode cur = this.head;
        if (this.head == null){
            System.out.println("链表为空");
            return;
        }
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
            count++;
            cur= cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        //要注意head是否为null
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        //如果链表内为空，尾插会空指针异常
        if (cur.next == null){
            this.head = node;
        }else{
            //查找尾节点的方式是看next是否为null
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //找到下标为index的地址所在位置
    public ListNode findIndex(int index){
        ListNode cur = this.head;
        while(index - 1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index > size()){
           return;
        }else if (index == 0){
            addFirst(data);
        }else if (index == size()){
            addLast(data);
        }else {
            ListNode node = new ListNode(data);
            ListNode cur = findIndex(index);
            node.next = cur.next;
            cur.next = node;
        }
    }

    //判断值为key时，它的地址为多少
    public ListNode searchPrev(int key){
        ListNode cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (this.head == null){
            System.out.println("链表为空");
            return;
        }
        if (this.head.val == key){//这之前也不要忘记链表为空
            this.head = this.head.next;
            return;
        }
        /*while (cur.next.val != key){
            cur = cur.next;
            if (cur.next == null){
                System.out.println("未找到该数字");
                return;
            }
        }*/
        ListNode cur = searchPrev(key);
        if (cur == null){
            System.out.println("没有你要删除的节点");
            return;
        }
        ListNode addCur = cur.next;
        cur.next = addCur.next;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        //删前判断链表是否为空
        if (this.head == null) return;

        ListNode cur = this.head.next;
        ListNode prev = this.head;

        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

        if (this.head.val == key){
            this.head = this.head.next;
        }
    }

    //有序链表插入一个x
    public void insIndex(int k){
        ListNode node = new ListNode(k);
        ListNode dummy = new ListNode(-1);
        if (this.head == null){
            this.head = node;
            return;
        }
        dummy.next = this.head;
        while (node.val >= dummy.val){
            if (dummy.next == null){
                dummy.next = node;
                return;
            }else if (dummy.next.val > node.val){
                break;
            }
            dummy = dummy.next;
        }
        node.next = dummy.next;
        if (dummy.next == this.head){
            this.head = node;
        }else {
            dummy.next = node;
        }
    }

    //清空链表
    public void clear(){
        //粗暴办法：
        //this.head = null;
        //其余办法：一个一个节点设为空
        //先把head存的下一节点设为空，head再往后进
        while (this.head != null){
            ListNode curNext = head.next;
            this.head.next = null;
            this.head = curNext;
        }
    }
}
